package com.foreknow.view;

import com.foreknow.model.Admin;
import com.foreknow.service.AdminService;
import com.foreknow.service.impl.AdminServiceImpl;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Login extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Login() {
        setTitle("\u767B\u5F55");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnNewButton = new JButton("\u767B\u5F55");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //登录的操作
                //调用Service中的登录方法
                AdminService service = new AdminServiceImpl();
                //获取到用户输入的数据: getText()方法
                String username = textField.getText();
                String password = passwordField.getText();
                Admin admin = service.login(username,password);
                if(admin!=null) {
                    System.out.println("login success......");
                }else {
                    System.out.println("login error......");
                }
            }
        });
        btnNewButton.setBounds(58, 208, 93, 23);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("\u6CE8\u518C");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //注册功能
            }
        });
        btnNewButton_1.setBounds(171, 208, 93, 23);
        contentPane.add(btnNewButton_1);

        JLabel label = new JLabel("\u7528\u6237\u540D: ");
        label.setBounds(87, 65, 54, 15);
        contentPane.add(label);

        JLabel label_1 = new JLabel("\u5BC6\u7801\uFF1A");
        label_1.setBounds(87, 122, 54, 15);
        contentPane.add(label_1);

        textField = new JTextField();
        textField.setBounds(146, 62, 206, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(145, 119, 207, 21);
        contentPane.add(passwordField);

        JButton button = new JButton("\u53D6\u6D88");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //取消
                textField.setText("");
            }
        });
        button.setBounds(286, 208, 93, 23);
        contentPane.add(button);
    }
}